<?php

namespace app\batch;

use think\console\Command;
use think\console\Input;
use think\console\Output;
use app\models\{User as Users, MachineLog};
use think\Db;
use app\facade\Redis;
use EasyWeChat\Factory;

class MachineFreed extends Command
{
    protected $open = false;
    protected function configure()
    {
        $this->setName('MachineFreed')->setDescription('矿机释放收益');
    }

    protected function execute(Input $input, Output $output)
    {
        $this->doCron();
        die();
    }

    protected function doCron()
    {
        $cmd = "ps aux | grep '/www/server/php/72/bin/php /www/wwwroot/rskj/think MachineFreed'|grep -v 'grep'| awk '{print $2}'";
        exec($cmd, $ps, $ret);
        if (count($ps) > 1) {
            die("batch of  is running");
        }
        ini_set('max_execution_time', 0);
        (new MachineLog())->where(['status' => 1])->where('lave_day', '>', 0)->where('days', '<', date('Y-m-d'))->chunk(100, function ($list) {
            foreach ($list as $lists) {
                $lave_day = $lists['lave_day'] - 1;
                $user = (new Users())->where('id', $lists['user_id'])->find();
                Db::startTrans();
                try {
                    (new MachineLog())->where('id', $lists['id'])->update([
                        'lave_day' => $lave_day,
                        'days' => date('Y-m-d H:i:s'),
                        'income' => ($lists['income'] + $lists['day_dividends']),
                        'todayincome' => $lists['day_dividends'],
                        'allincome' => ($lists['allincome'] + $lists['day_dividends'])
                    ]);
                    Db::commit();
                } catch (\Exception $e) {
                    Db::rollback();
                    echo $e->getMessage();
                }
                echo '成功';
            }
        });
    }
}
